<template>
	<view class="main">
		<!-- 		<view class="hd-box" style="background: url('/static/imgs/bgc-top01.png') no-repeat; background-size: cover;">
			<view class="hd-left">
				<image src="/static/imgs/positioning.png" mode="aspectFill" class="position-icon"></image>
				<view @click="checkAddress()">{{mineAddress}}</view>
				<text class="iconfont" style="color: #fff;">&#xe627;</text>
			</view>
			<view class="hd-right">
				<view class="r-item" @click="navTo('/pages/public/news')">
					<image src="/static/imgs/message.png" mode="aspectFill" class="message"></image>
					<view class="point">
						{{ label.messageNums | handleLabel}}
					</view>
				</view>
				<view class="r-item" @click="navTo('/pages/public/collect')">
					<image src="/static/imgs/collection.png" mode="aspectFill" class="collection"></image>
					<view class="point">
						{{ label.collectCountNums | handleLabel}}
					</view>
				</view>
				<view class="r-item" @click="navTo('/pages/discountMall/shopCart')">
					<image src="/static/imgs/home-shoppingcart.png" mode="aspectFill" class="shoppingcart"></image>
					<view class="point">
						{{ label.cartCountNums | handleLabel}}
					</view>
				</view>
			</view>
		</view> -->

		<!-- 搜索部分 -->
		<view class="in-header" style="background: url('/static/imgs/bgc04.png') no-repeat; background-size: cover;">
			<!-- search 框 -->
			<view class="search-box" @click="getBanner">
				<view class="sb-left" @click="afterFlag = !afterFlag">
					<text class="iconfont">&#xe6b2;</text>
					<view class="left-footer">
						核销
					</view>
				</view>
				<text class="iconfont search">&#xe62e;</text>
				<input type="text" disabled="true" @click="navTo('/pages/index/search')" placeholder="护肤品" placeholder-class="search-input" />

				<!-- 核销弹窗部分 112 160-->
				<view class="popup-after" v-show="afterFlag" style="background: url('/static/imgs/index/background.png') no-repeat; background-size: contain;">
					<view class="line mt" @click="onScan">
						扫一扫
					</view>
					<view class="b"></view>
					<view class="line" @click="navTo('/pages/index/cancelAfter')">
						请输入
					</view>
				</view>

			</view>

		</view>

		<!-- banner 轮播 -->
		<view class="banner">

			<uni-swiper-dot :info="bannerList" :current="current" mode="round" :dots-styles="dotsStyles" field="content">
				<swiper class="swiper-box" @change="change" autoplay="false" interval="5000" circular="true">

					<swiper-item v-for="(item,index) in bannerList" :key="index">
						<view class="uni-bg-blue swiper-item">
							<image :src="item.banimg" mode="aspectFill" />
						</view>
					</swiper-item>

				</swiper>
			</uni-swiper-dot>
		</view>

		<!-- 品牌描述-->
		<view class="brand">
			<view class="brand-box">
				<view class="box-item">
					<image src="/static/imgs/brand1.png" mode="aspectFill"></image>
					<text>邻近特价</text>

				</view>
				<view class="box-item">
					<image src="/static/imgs/brand2.png" mode="aspectFill"></image>
					<text>货真价实</text>

				</view>
				<view class="box-item">
					<image src="/static/imgs/brand3.png" mode="aspectFill"></image>
					<text>快速到手</text>
				</view>
				<view class="box-item">
					<image src="/static/imgs/brand4.png" mode="aspectFill"></image>
					<text>无忧无虑</text>
				</view>
			</view>
		</view>
		<tui-sticky :scrollTop="scrollTop" stickyHeight="128">
			<template v-slot:header>
				<view class="sort">
					<view class="scroll2">
						<view @click="ontabtap($event,item.cateid)" class="sort-item" v-for="(item,index) in sortList" :key="index" :id="item.cateid"
						 :data-current="item.cateid">
							<text :class="tabIndex==item.cateid ? 'sort-active' : ''">{{item.catename}}</text>
						</view>
					</view>
				</view>
				<!-- <view class="sticky-item">
					<view @click="ontabtap($event,item.cateid)" class="sort-item" v-for="(item,index) in sortList" :key="index" :id="item.cateid"
					 :data-current="item.cateid">
						<text :class="tabIndex==item.cateid ? 'sort-active' : ''">{{item.catename}}</text>
					</view>
				</view> -->
			</template>
			<template v-slot:content>
				<!--内容 start-->
				<view class="goods-box">
					<!-- 	<view class="list-item" hover-class="hover" :hover-stay-time="150" bindtap="detail" v-for="(val,index) in allData" :key="index">
						<view class="content-box">
							<view class="des-box">
								<view class="tit">朝朝盈-2019.06.03asdfasdfadsfasdf</view>
								<view class="source">理财</view>
								<view class="time">今天 09:01</view>
							</view>
						</view>
						<view class="money add">
							+0.07
						</view>
					</view> -->
					<view class="noDatePage" v-if="Nodate">
						<image src="/static/split-new/indexNo.png" mode=""></image>
					</view>
					<view class="noDatePage" v-if="allData.length == 0">
						<text>暂无商品</text>
					</view>
					<view v-if="!Nodate" class="goods-item" v-for="(val,index) in allData" :key="index">
						<view class="sort1" v-if="val.product_cateid == 5">
							<image class="miaosha" src="/static/imgs/index/miaosha.png" mode="aspectFill"></image>
						</view>

						<view class="sort1" v-if="val.product_cateid == 8">
							<image class="pingtuan" src="/static/imgs/index/pingtuan.png" mode="aspectFill"></image>
						</view>

						<view class="sort1" v-if="val.product_cateid == 9">
							<image class="ttc" src="/static/imgs/index/ttc.png" mode="aspectFill"></image>
						</view>

						<view class="sort1" v-if="val.product_cateid == 10">
							<image class="ershou" src="/static/imgs/index/ershou.png" mode="aspectFill"></image>
							<view class="num">
								3888.1
							</view>
						</view>

						<image :src="val.product_proimgs" @click="navTo(`/pages/detail/detail?proid=${val.product_id}`)" mode="aspectFill"
						 class="item-img"></image>
						<view class="item-title">
							{{val.product_name}}
						</view>
						<view class="item-msg">
							<text class="price-1">￥<text class="price-1-1">{{val.product_nowprice * 1}}</text></text>
							<text class="price-2">￥{{val.product_preprice * 1}}</text>
							<text class="msg-3">仅剩{{val.product_pronums}}</text>
							<text class="msg-4" @click="navToOrder(val)">抢购</text>
						</view>

						<view class="item-position">
							<view class="left">
								<image :src="val.shop_shavatar" mode="aspectFill" class="user-icon"></image>
								<text class="position-1">{{val.shop_name}}</text>
							</view>
							<view class="right">
								<image src="/static/imgs/people.png" mode="aspectFill" class="person-icon"></image>
								<text class="position-3">{{val.distance}}米</text>
							</view>
						</view>

						<view class="item-footer">
							<view class="footer-l">
								<block v-for="(img,sub) in val.order_user_shavatar" :key="sub">
									<image :src="img.shavatar" mode="aspectFill" class="f-image-1"></image>
								</block>
							</view>
							<view class="footer-r">
								等邻居都在抢
							</view>
						</view>
					</view>

				</view>
				<!--内容 end-->
			</template>
		</tui-sticky>
		<!-- 	<tui-sticky @change="changeScrollY" :scrollTop="scrollTop" :stickyTop="133" stickyHeight="100rpx">
			<template v-slot:header style="height: 10upx;">
				<view class="sort">
					<view class="scroll2">
						<view @click="ontabtap($event,item.cateid)" class="sort-item" v-for="(item,index) in sortList" :key="index" :id="item.cateid"
						 :data-current="item.cateid">
							<text :class="tabIndex==item.cateid ? 'sort-active' : ''">{{item.catename}}</text>
						</view>
					</view>
				</view>
			</template>
			<template v-slot:content>
				<swiper class="swiper-box1" :current="scrollInto" @change="ontabChange" :indicator-dots="false" :autoplay="false"
				 duration="300">
					<swiper-item style="height: 100%;" v-for="(a,subs) in sortList" :key="subs">
						<scroll-view @scrolltoupper="scrolltoupper" class="scroll-view scroll-v" :scroll-y="scrollY">
							<view class="goods-box">
								<view class="goods-item" v-for="(val,index) in allData" :key="index">
									<view class="sort1" v-if="val.product_cateid == 5">
										<image class="miaosha" src="/static/imgs/index/miaosha.png" mode="aspectFill"></image>
									</view>

									<view class="sort1" v-if="val.product_cateid == 8">
										<image class="pingtuan" src="/static/imgs/index/pingtuan.png" mode="aspectFill"></image>
									</view>

									<view class="sort1" v-if="val.product_cateid == 9">
										<image class="ttc" src="/static/imgs/index/ttc.png" mode="aspectFill"></image>
									</view>

									<view class="sort1" v-if="val.product_cateid == 10">
										<image class="ershou" src="/static/imgs/index/ershou.png" mode="aspectFill"></image>
										<view class="num">
											3888.1
										</view>
									</view>

									<image :src="val.product_proimgs" @click="navTo(`/pages/detail/detail?proid=${val.product_id}`)" mode="aspectFill"
									 class="item-img"></image>
									<view class="item-title">
										{{val.product_name}}
									</view>
									<view class="item-msg">
										<text class="price-1">￥<text class="price-1-1">{{val.product_nowprice * 1}}</text></text>
										<text class="price-2">￥{{val.product_preprice * 1}}</text>
										<text class="msg-3">仅剩{{val.product_pronums}}</text>
										<text class="msg-4" @click="navToOrder(val.shop_id)">抢购</text>
									</view>

									<view class="item-position">
										<view class="left">
											<image :src="val.shop_shavatar" mode="aspectFill" class="user-icon"></image>
											<text class="position-1">{{val.shop_name}}</text>
										</view>
										<view class="right">
											<image src="/static/imgs/people.png" mode="aspectFill" class="person-icon"></image>
											<text class="position-3">{{val.distance}}米</text>
										</view>
									</view>

									<view class="item-footer">
										<view class="footer-l">
											<block v-for="(img,sub) in val.order_user_shavatar" :key="sub">
												<image :src="img.shavatar" mode="aspectFill" class="f-image-1"></image>
											</block>
										</view>
										<view class="footer-r">
											等邻居都在抢
										</view>
									</view>
								</view>
							</view>
						</scroll-view>
					</swiper-item>
				</swiper>
			</template>
		</tui-sticky> -->

		<!-- <view class="box-demo"></view> -->
		<view class="mask" v-show="afterFlag" @click="onMask" @touchmove="onMask"></view>

		<!-- 弹窗1 -->
		<QSPopup ref="QSPopup1" type="fadeInMiddle">
			<view class="check failed">
				<view class="line1">
					提货码核对失败！
				</view>
				<view class="line2">
					{{errMsg}}
				</view>
				<button type="primary" @click="onConfirm">确认</button>
			</view>
		</QSPopup>
		<!-- 弹窗2 -->
		<QSPopup ref="QSPopup2" type="fadeInMiddle">
			<view class="check failed">
				<view class="line3">
					核销成功！
				</view>

				<button type="primary" @click="onConfirm" class="confirmButtom">确认</button>
			</view>
		</QSPopup>


	</view>

</template>

<script>
	var bitmap = null;
	import uniSwiperDot from '@/components/uni-swiper-dot/uni-swiper-dot.vue'
	import QSPopup from '@/components/QS-popup/QS-popup.vue'
	import tuiSticky from "@/components/sticky/sticky"
	import { mapState, mapMutations } from 'vuex'

	export default {
		data() {
			return {
				Nodate: false,
				scrollTop: 0,
				scrollY: false,
				afterFlag: false, //核销
				hxValue: '', // 核销码
				errMsg: '请重新输入提货码！', // 核销失败提示
				mineAddress: '请选择位置', // 位置获取
				LL: { long: '114.121224', lat: '22.627038' }, // 经纬度
				// banner
				bannerList: [], // 轮播数据
				sortList: [
					{ value: 'a' },
					{ value: '2' },
					{ value: '3' },
					{ value: '4' },
					{ value: '5' }
				], // 分类数据
				sortList1: [
					{ id: 'one', name: '9.9专区' },
					{ id: 'two', name: '限时秒杀' },
					{ id: 'three', name: '拼团' },
					{ id: 'fore', name: '土特产' },
					{ id: 'five', name: '二手' },
					{ id: 'six', name: '更多···' }
				],
				allData: [], // 所有数据
				zq9: [], // 9.9专区
				seckill: [], // 秒杀
				fight: [], // 拼团
				local: [], // 土特产
				usedGood: [], // 二手
				more: [], //更多
				label: {}, // 点标记

				tabIndex: 1, // 分类ID
				scrollInto: 0, // 轮播的位置
				current: 0,
				mode: 'nav',
				dotsStyles: {
					backgroundColor: 'rgba(255, 255, 255, .4)',
					border: '0px rgba(255, 255, 255, .4) solid',
					color: '#fff',
					selectedBackgroundColor: 'rgba(255, 255, 255, 1)',
					selectedBorder: '0px rgba(255, 255, 255, 1) solid'
				}
			}
		},
		onNavigationBarButtonTap(e) {
			let url = ""
			if (e.index === 1) {
				this.checkAddress()
			} else if (e.index === 2) {
				url = '/pages/discountMall/shopCart'
			} else if (e.index === 3) {
				url = '/pages/public/collect'
			} else if (e.index === 4) {
				url = '/pages/public/news'
			}
			this.navTo(url)
		},
		onPageScroll(e) {
			this.scrollTop = e.scrollTop
		},
		onLoad() {
			this.getBanner();
			this.getSort();
			this.handleAddress();
			this.getShopMessage()
			this.getLL();
			this.getData();
		},
		filters: {
			handleLabel: (val) => {
				let a = Number(val)
				if (a < 100) {
					a = Number(val);
				} else {
					a = '···'
				}
				return a;
			}
		},
		onPullDownRefresh() {
			// uni.startPullDownRefresh()
			this.getBanner();
			this.getSort();
			this.handleAddress();
			this.getShopMessage()
			this.getLL();
			this.getData();
			setTimeout(() => {
				uni.stopPullDownRefresh();
				uni.showToast({
					title: '刷新成功',
					icon: "none"
				});
			}, 1000)
		},
		onShow() {
			let token = uni.getStorageSync('lph-token');
			if (token) {
				this.getCartAndCollectNums();
			}
		},
		methods: {
			...mapMutations(['changeLogin', 'changeUserInfo']),
			async getShopMessage(){
				let userRes = uni.getStorageSync('lph-user-info')
				let result = await this.$http.getShopMessageUnreadCount({
					user_id:userRes.userid
				})
				this.setBadge(4,result.data.count)
			},
			changeScrollY(res) {
			
				// this.scrollY = res ? true : false
				if (res == true) {
					this.scrollY = true
				} else {
					this.scrollY = false
				}
			},
			scrolltoupper(e) {
				this.scrollY = false
			},
			async getData() { // 获取数据
				console.log(this.tabIndex);
				let a = await this.$http.getProductListView({
					lng: this.LL.long,
					lat: this.LL.lat,
					cate_id: this.tabIndex
				});
				this.allData = a.data;
			},
			async getSort() { // 获取分类，
				let sort = await this.$http.getIndexSort();
				this.sortList = sort.data;
			},
			async getCartAndCollectNums() { // 点标记数据
				let a = await this.$http.getCartAndCollectNums();
				this.label = a.data;
			},
			async saveDefaultAddress(result) { // 设置地址
				let a = await this.$http.saveDefaultAddress(result);
			},


			onScan() { // 点击扫一扫
				this.afterFlag = false;
				uni.scanCode({
					success: (res) => {
						this.$http.scanCode({
							pullword: res.result
						}).then((a) => {
							if (a.code == 200) {
								this.$refs['QSPopup2'].show();
							} else {
								this.errMsg = a.message;
								this.$refs['QSPopup1'].show();
							}
						})
					}
				})
			},
			onConfirm() { // 核销弹窗内的确定
				this.$refs['QSPopup1'].hide();
				this.$refs['QSPopup2'].hide();
			},
			getLL() { // 获取经纬度，并保存到缓存中 
				// #ifdef APP-PLUS
				uni.getLocation({
					type: 'wgs84',
					geocode:true,
					success: res => {
						uni.setStorageSync("lph-ll", {
							long: res.longitude,
							lat: res.latitude
						});
						this.LL = {
							long: res.longitude,
							lat: res.latitude,
							name:res.address.poiName
						}
						this.mineAddress = this.LL.name
						this.setStyle(this.mineAddress)
					}
				});
				// #endif

				// #ifndef APP-PLUS
				uni.setStorageSync("lph-ll", {
					long: '114.121224',
					lat: '22.627038'
				});
				this.LL = {
					long: '114.121224',
					lat: '22.627038'
				};
				// #endif

			},
			setBadge(index, text) {
				// #ifdef APP-PLUS
				let webView = this.$mp.page.$getAppWebview()
				webView.setTitleNViewButtonBadge({  
					index, text
				})
				// #endif
			},
			checkAddress() { // 选择位置
				uni.chooseLocation({
					success: (res) => {
						console.log(res)
						this.mineAddress = res.name;
						this.setStyle(res.name)
						uni.setStorageSync('lph-address', {
							name: res.name,
							long: res.longitude,
							lat: res.latitude
						});
						// let result = {
						// 	currentAdresss: '',
						// 	lng: '',
						// 	lat: ''
						// }
						// this.
					}
				})
			},
			// 轮播数据
			async getBanner() { // 获取轮播数据
				let a = await this.$http.getBannerList();
				if (a.code == 200) {
					this.bannerList = a.data;
				}
			},

			navTo(url) {
				this.afterFlag = false;
				uni.navigateTo({
					url: url
				})
			},
			async navToOrder(val) { // 加入购物车
				// console.log(val.product_id)
				let proid = val.product_id
				// let data = {
				// 	or_proid: val.proid,
				// 	or_shopid: val.shopid,
				// 	or_pronum: 1,
				// };
				// let a = await this.$http.addOrder(data);
				uni.navigateTo({
					url: `/pages/discountMall/finishOrder?pro_id=${proid}&quantity=1`
				})
			},
			ontabChange(e) { // banner 切换的
				let index = e.target.current || e.detail.current;
				this.tabIndex = [1, 5, 8, 9, 10][index];
				this.getData();
			},
			ontabtap(e, cateid) { // nav 切换的
				uni.showLoading({
					title: '加载中'
				});
				console.log(e)
				console.log(cateid)
				this.tabIndex = cateid;
				console.log(this.tabIndex)
				if (this.tabIndex == 5 || this.tabIndex == 8 || this.tabIndex == 9 || this.tabIndex == 10) {
					this.Nodate = true
				} else {
					this.Nodate = false
					this.getData()
				}
				uni.hideLoading();
				// this.scrollInto = [1, 5, 8, 9, 10].indexOf(cateid);
			},

			// 改变轮播点标记的
			change(e) {
				this.current = e.detail.current
			},

			onMask() { //  蒙版的处理
				this.afterFlag = false
			},
			handleAddress() { // 处理地址
				let a = uni.getStorageSync("lph-address");
				if ('name' in a) {
					this.mineAddress = a.name;
					return
				}
				this.mineAddress = '请选择地址'
			},

			// 导航栏
			/**
			 * 修改导航栏buttons
			 * text[string] 需要修改的text 内容
			 */
			setStyle(text) {
				let pages = getCurrentPages();
				let page = pages[pages.length - 1];
				if (text.length > 3) {
					text = text.substr(0, 3) + '...';
				}
				// #ifdef APP-PLUS
				let currentWebview = page.$getAppWebview();  
				currentWebview.setTitleNViewButtonStyle(1, {
					text
				})
				// #endif
				
				// #ifdef H5
				// h5 临时方案
				document.getElementsByClassName('uni-btn-icon')[1].innerText = text;
				// #endif
			}
		},
		components: { uniSwiperDot, QSPopup, tuiSticky }
	}
</script>

<style lang="scss" scoped>
	@import 'static/css/index.scss';
	@import 'static/css/doenCommon.scss';
	.uni-page-head__title{
		color: #FFFFFF !important;
	}

	.noDatePage {
		width: 100%;
		display: flex;
		align-items: center;
		justify-content: center;
		&>text{
			font-size: 30upx;
			color:#999999;
			padding-top: 200upx;
		}
	}

	.container {
		padding-bottom: env(safe-area-inset-bottom);
	}

	.tips {
		background: #fff;
		padding: 50rpx 34rpx;
		color: #555;
		font-size: 24rpx;
		line-height: 42rpx;
		position: relative;
	}

	.tips::before {
		content: '';
		position: absolute;
		border-top: 1rpx solid #eaeef1;
		-webkit-transform: scaleY(0.5);
		transform: scaleY(0.5);
		top: 0;
		right: 0;
		left: 0;
	}

	/* .sticky-container{
	 position: relative;
	} */
	/* sticky 容器 start */
	.sticky-item {
		width: 100%;
		height: 104rpx;
		padding: 20rpx 30rpx;
		background: #fafafa;
		display: flex;
		align-items: center;
		justify-content: space-between;
		box-sizing: border-box;
	}

	/* sticky 容器 end */

	.date {
		height: 54rpx;
		font-size: 28rpx;
		background: #fff;
		padding: 0 28rpx;
		border-radius: 25rpx;
		display: flex;
		align-items: center;
		justify-content: center;
		position: relative;
		/* box-shadow: 0 0 1rpx #7A7A7A; */
	}

	.date::after {
		content: '';
		position: absolute;
		height: 200%;
		width: 200%;
		border: 1rpx solid #eaeef1;
		border-radius: 60rpx;
		-webkit-transform-origin: 0 0;
		transform-origin: 0 0;
		-webkit-transform: scale(0.5);
		transform: scale(0.5);
		left: 0;
		top: 0;
	}

	.amount {
		text-align: right;
		color: #7A7A7A;
		font-size: 24rpx;
	}

	.list-view {
		position: relative;
		width: 100%;
		overflow: hidden;
		// margin-bottom: 150upx;
		border: 1px solid red;
		padding-bottom: 200upx;
		display: flex;
		flex-wrap: wrap;
		justify-content: space-between;
		padding-left: 15upx;
		padding-right: 15upx;
		margin-bottom: 100upx;
	}

	.list-view::after {
		content: '';
		position: absolute;
		border-top: 1rpx solid #eaeef1;
		-webkit-transform: scaleY(0.5);
		transform: scaleY(0.5);
		top: 0;
		right: 0;
		left: 0;
	}

	.list-item {
		// width: 100%;
		// padding: 30rpx 28rpx;
		// box-sizing: border-box;
		// background: #fff;
		// display: flex;
		// align-items: flex-start;
		// justify-content: space-between;
		border: 1px solid red;

		flex: 1;
		min-width: 346upx;
		max-width: 346upx;
		// height: 562upx;
		background-color: #fff;
		border-radius: 10upx;
		margin-top: 20upx;
		position: relative;

	}

	.list-item::after {
		left: 120rpx
	}

	.item-last::after {
		left: 0 !important
	}

	.content-box {
		display: flex;
		align-items: flex-start;
		justify-content: space-between;
	}

	.logo {
		width: 60rpx;
		height: 60rpx;
		flex-grow: 1
	}

	.des-box {
		min-height: 130rpx;
		padding-left: 28rpx;
		box-sizing: border-box;
		vertical-align: top;
		color: #333;
		font-size: 24rpx;
		display: flex;
		flex-direction: column;
		justify-content: space-between;
	}

	.tit {
		font-size: 32rpx;
		max-width: 270rpx;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
	}

	.time {
		color: #888
	}

	.money {
		font-size: 38rpx;
		font-weight: 500;
		color: #000;
		white-space: nowrap;
		overflow: hidden;
		text-overflow: ellipsis;
		padding-left: 20rpx;
	}

	.add {
		color: #5677FC !important;
	}
</style>
